<template>
  <basic-dialog
    :visible.sync="pushVisible"
    :title="'批量推送询价单'"
    :ok-text="'推送'"
    :handle-close="handleCloseDialog"
    :handle-ok="handlePush"
    :loadingBtn="pushLoading"
  >
    <div>
      <el-form ref="inquiryForm" :model="inquiryForm">
        <el-form-item label="报价有效期：" prop="offerEndTime">
          <el-date-picker
            v-model="inquiryForm.offerEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="'23:59:59'"
            :picker-options="pickerOptions"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
			</el-form>
      <basic-list
        :data-source="qualifieList"
        :tableHeight="300"
        :columns="columns"
        :show-select-col="false"
        :show-pagination="false"
      />
    </div>
  </basic-dialog>
</template>
<script>
import { mapState } from 'vuex'
import BasicDialog from '@/pages/sourcing/components/BasicDialog';
import BasicList from '@/pages/sourcing/components/BasicList';
import { pushInquiryService } from '@api/sourcing/selfPurchaseInquiry'

export default {
  // 自进货推送询价到协同
  name: 'PushInquiryDialog',
  props: ['pushVisible', 'dialogData', 'selfPurchaseBillId'],
  components: {
    BasicDialog,
    BasicList
  },
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() < new Date().getTime() - 86400000;
        }
      },
      pushLoading: false,
      inquiryForm: {
        offerEndTime: ''
      },
      columns: [
        {
          prop: 'supplierName',
          label: '供应商名称'
        },
        {
          prop: 'supplierCode',
          label: '供应商代码'
        },
        {
          prop: 'supplierLinkman',
          label: '供应商联系人'
        },
        {
          prop: 'supplierPhone',
          label: '供应商电话'
        }
      ]
    };
  },
  computed: {
    ...mapState('sourcing/selfPurchase', {
      remark: (state) => state.remark
    }),
    qualifieList () {
      return this.dialogData;
    }
  },
  methods: {
    async handlePush () {
      const { offerEndTime } = this.inquiryForm;
      if (!offerEndTime) {
        return this.$message.warning('请选择报价有效期')
      }
      if (Math.round(new Date(offerEndTime) / 1000) < Date.parse(new Date()) / 1000) {
        return this.$message.warning('报价有效期不能小于当前时间')
      }
      const params = {
        selfPurchaseBillId: this.selfPurchaseBillId,
        offerEndTime,
        sourcingSupplierIds: this.qualifieList.map(item => item.sourcingSupplierId),
        remark: this.remark
      }
      const res = await pushInquiryService(params, state => { this.pushLoading = state })
      if (res) {
        this.$message.success('推送成功')
        this.$emit('getSupplierList')
        this.handleCloseDialog()
      }
    },
    handleCloseDialog () {
      this.$emit('update:pushVisible', false);
    }
  }
};
</script>
